package com.iplant.ams.serviceImpl;

import com.iplant.ams.service.DMSService;
import com.iplant.ams.service.basicenum.dms.scada.DMSDataActions;
import com.iplant.ams.service.basicenum.dms.scada.DMSDataClass;
import com.iplant.ams.service.basicenum.dms.scada.DMSDataTypes;
import com.iplant.ams.service.po.dms.DMSDeviceLedger;
import com.iplant.ams.service.po.dms.scada.alarm.DMSDeviceAlarm;
import com.iplant.ams.service.po.dms.scada.alarm.DMSDeviceAlarmFrequency;
import com.iplant.ams.service.po.dms.scada.alarm.DMSDeviceAlarmStatistics;
import com.iplant.ams.service.po.dms.scada.alarm.DMSDeviceCurrentAlarm;
import com.iplant.ams.service.po.dms.scada.conf.DMSDeviceParameter;
import com.iplant.ams.service.po.dms.scada.para.DMSDeviceRealParameter;
import com.iplant.ams.service.po.dms.scada.record.DMSProcessRecord;
import com.iplant.ams.service.po.dms.scada.record.DMSProcessRecordItem;
import com.iplant.ams.service.po.dms.scada.status.DMSDeviceAreaStatus;
import com.iplant.ams.service.po.dms.scada.status.DMSDeviceStatistics;
import com.iplant.ams.service.po.dms.scada.status.DMSDeviceStatus;
import com.iplant.ams.service.po.dms.scada.status.DMSDeviceStatusStatistics;
import com.iplant.ams.serviceImpl.dao.dms.DMSDeviceLedgerDAO;
import com.iplant.ams.serviceImpl.dao.dms.DMSDeviceParameterDAO;
import com.iplant.ams.serviceImpl.dao.dms.scada.*;
import com.iplant.ams.serviceImpl.utils.DMSDeviceStatusEnumTool;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.cache.CacheUtils;
import com.iplant.base.utils.general.CalendarTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.basicenum.stat.StatTypes;
import com.iplant.basic.service.po.bms.BMSEmployee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Service
public class DMSServiceImpl implements DMSService {


    public DMSServiceImpl() {
        // TODO Auto-generated constructor stub
    }

    private static DMSService Instance = null;

    public static DMSService getInstance() {
        if (Instance == null)
            Instance = new DMSServiceImpl();

        return Instance;
    }

    @Override
    public ServiceResult<Map<Integer, Map<String, Integer>>> DMS_SelectDeviceStatusTime(BMSEmployee wLoginUser, List<Integer> wID, String wCode, String wName,
                                                                                        List<String> wAssetNoList, int wDeviceType, int wModelID, int wFactoryID, int wWorkShopID, int wAreaID,
                                                                                        Calendar wStartTime, Calendar wEndTime) {
        ServiceResult<Map<Integer, Map<String, Integer>>> wResult = new ServiceResult<Map<Integer, Map<String, Integer>>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wResult.Result = DMSDeviceStatusDAO.getInstance().DMS_SelectDeviceStatusTime(wLoginUser, wID,
                    wCode, wName, wAssetNoList, wDeviceType, wModelID, wFactoryID, wWorkShopID, wAreaID,
                    wStartTime, wEndTime, wErrorCode);
            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceStatusTime", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSDeviceAreaStatus>> DMS_CurrentDeviceStatusStatistics(BMSEmployee wLoginUser, String wDeviceName,
                                                                                      String wAssetNo, int wDeviceType,
                                                                                      int wModelID, int wFactoryID, int wWorkShopID, int wAreaID, int wStatus, Pagination wPagination) {
        ServiceResult<List<DMSDeviceAreaStatus>> wResult = new ServiceResult<List<DMSDeviceAreaStatus>>();
        try {
            wResult.Result = new ArrayList<DMSDeviceAreaStatus>();
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);


            List<DMSDeviceStatus> wDeviceStatusList = DMSDeviceStatusDAO.getInstance().DMS_CurrentDeviceStatusList(wLoginUser, StringUtils.parseListArgs(-1), "", wDeviceName,
                    StringUtils.parseListArgs(wAssetNo), wDeviceType, wModelID, wFactoryID, wWorkShopID, wAreaID, wStatus, CalendarTool.getBasicDate(), CalendarTool.getBasicDate(), wPagination, wErrorCode);

            wResult.AddFaultCode(wErrorCode);
            if (wDeviceStatusList == null || wDeviceStatusList.size() == 0 || wErrorCode.Result != 0) {
                return wResult;
            }

            Map<Integer, List<DMSDeviceStatus>> wDeviceStatusListDic = wDeviceStatusList.stream().collect(Collectors.groupingBy(p -> p.AreaID));


            List<Integer> wStatusList = DMSDeviceStatusDAO.mStatusList;

            DMSDeviceAreaStatus wDeviceAreaStatus = null;
            for (int wArea : wDeviceStatusListDic.keySet()) {
                if (!wDeviceStatusListDic.containsKey(wArea) || wDeviceStatusListDic.get(wArea) == null || wDeviceStatusListDic.get(wArea).size() <= 0)
                    continue;
                wDeviceAreaStatus = new DMSDeviceAreaStatus();
                wDeviceAreaStatus.AreaID = wArea;
                wDeviceAreaStatus.AreaName = wDeviceStatusListDic.get(wArea).get(0).PositionText;
                wDeviceAreaStatus.AreaNo = wDeviceStatusListDic.get(wArea).get(0).AreaNo;
                wDeviceAreaStatus.DeviceCount = wDeviceStatusListDic.get(wArea).size();

                for (Integer wStatusTemp : wStatusList) {
                    wDeviceAreaStatus.StatusCount.put(wStatusTemp.toString(), (int) wDeviceStatusListDic.get(wArea).stream().filter(p -> (p.Status & wStatusTemp) > 0).count());
                }
                wResult.Result.add(wDeviceAreaStatus);
            }
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_CurrentDeviceStatusStatistics", ex);
        }
        return wResult;


    }

    @Override
    public ServiceResult<List<DMSDeviceStatus>> DMS_CurrentDeviceStatusList(BMSEmployee wLoginUser, int wDeviceID, String wDeviceNo, String wDeviceName,
                                                                            String wAssetNo, int wDeviceType, int wModelID, int wFactoryID, int wWorkShopID, int wAreaID, int wStatus, Pagination wPagination) {
        ServiceResult<List<DMSDeviceStatus>> wResult = new ServiceResult<List<DMSDeviceStatus>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wResult.Result = DMSDeviceStatusDAO.getInstance().DMS_CurrentDeviceStatusList(wLoginUser, StringUtils.parseListArgs(wDeviceID), wDeviceNo, wDeviceName,
                    StringUtils.parseListArgs(wAssetNo), wDeviceType, wModelID, wFactoryID, wWorkShopID, wAreaID, wStatus, CalendarTool.getBasicDate(), CalendarTool.getBasicDate(), wPagination, wErrorCode);


            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_CurrentDeviceStatusList", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSDeviceStatus>> DMS_SelectDeviceStatusList(BMSEmployee wLoginUser, String wName,
                                                                           String wAssetNo, int wDeviceType, int wModelID, int wFactoryID, int wWorkShopID, int wAreaID,
                                                                           int wActive, Calendar wStartTime, Calendar wEndTime, Pagination wPagination) {
        ServiceResult<List<DMSDeviceStatus>> wResult = new ServiceResult<List<DMSDeviceStatus>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            List<DMSDeviceStatus> wServerRst = DMSDeviceStatusDAO.getInstance().DMS_SelectDeviceStatusList(wLoginUser, wName,
                    wAssetNo, wDeviceType, wModelID, wFactoryID, wWorkShopID, wAreaID, wActive,
                    wStartTime, wEndTime, wPagination, wErrorCode);
            wResult.setResult(wServerRst);

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceStatusList", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSDeviceStatus>> DMS_SelectDeviceStatus(BMSEmployee wLoginUser, int wID, String wCode,
                                                                       String wAssetNo, Calendar wStartTime, Calendar wEndTime, Pagination wPagination) {
        ServiceResult<List<DMSDeviceStatus>> wResult = new ServiceResult<List<DMSDeviceStatus>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            List<DMSDeviceStatus> wServerRst = DMSDeviceStatusDAO.getInstance().DMS_SelectDeviceStatus(wLoginUser, wID, wCode,
                    wAssetNo, wStartTime, wEndTime, wPagination, wErrorCode);
            wResult.setResult(wServerRst);

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceStatus", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSDeviceStatus>> DMS_SelectDeviceStatusList(BMSEmployee wLoginUser,
                                                                           List<Integer> wIDList, Calendar wStartTime, Calendar wEndTime, Pagination wPagination) {

        ServiceResult<List<DMSDeviceStatus>> wResult = new ServiceResult<List<DMSDeviceStatus>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            List<DMSDeviceStatus> wServerRst = DMSDeviceStatusDAO.getInstance().DMS_SelectDeviceStatusList(wLoginUser,
                    wIDList, wStartTime, wEndTime, wPagination, wErrorCode);
            wResult.setResult(wServerRst);

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceStatusList", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSDeviceStatus>> DMS_SelectDeviceStatusDetailList(BMSEmployee wLoginUser, String wName, int wDeviceID,
                                                                                 String wAssetNo, int wDeviceType, int wModelID, int wFactoryID, int wWorkShopID, int wAreaID,
                                                                                 List<Integer> wStatus, Calendar wStartTime, Calendar wEndTime, Pagination wPagination) {
        ServiceResult<List<DMSDeviceStatus>> wResult = new ServiceResult<List<DMSDeviceStatus>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            List<DMSDeviceStatus> wServerRst = DMSDeviceStatusDAO.getInstance().DMS_SelectDeviceStatusDetailList(wLoginUser, wName, wDeviceID,
                    wAssetNo, wDeviceType, wModelID, wFactoryID, wWorkShopID, wAreaID,
                    wStatus, wStartTime, wEndTime, wPagination, wErrorCode);
            wResult.setResult(wServerRst);

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceStatusDetailList", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSDeviceStatus>> DMS_SelectDeviceStatusDetail(BMSEmployee wLoginUser, int wDeviceID, String wDeviceNo,
                                                                             String wAssetNo, List<Integer> wStatus, Calendar wStartTime, Calendar wEndTime, Pagination wPagination) {
        ServiceResult<List<DMSDeviceStatus>> wResult = new ServiceResult<List<DMSDeviceStatus>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            wResult.Result = DMSDeviceStatusDAO.getInstance().DMS_SelectDeviceStatusDetail(wLoginUser, wDeviceID, wDeviceNo,

                    wAssetNo, wStatus, wStartTime, wEndTime, wPagination, wErrorCode);

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceStatusDetail", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSDeviceStatus>> DMS_SelectDeviceStatusDetailList(BMSEmployee wLoginUser,
                                                                                 List<Integer> wDeviceIDList, List<Integer> wStatus, Calendar wStartTime, Calendar wEndTime, Pagination wPagination) {

        ServiceResult<List<DMSDeviceStatus>> wResult = new ServiceResult<List<DMSDeviceStatus>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            List<DMSDeviceStatus> wServerRst = DMSDeviceStatusDAO.getInstance().DMS_SelectDeviceStatusDetailList(wLoginUser,
                    wDeviceIDList, wStatus, wStartTime, wEndTime, wPagination, wErrorCode);
            wResult.setResult(wServerRst);

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceStatusDetailList", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSDeviceAlarm>> DMS_CurrentDeviceAlarmList(BMSEmployee wLoginUser, int wDeviceID, String wDeviceNo, String wAssetNo,
                                                                          int wDeviceType, int wModelID, int wFactoryID, int wWorkShopID, int wAreaID, String wAlarmCode, int wDataGrad, int wPositionID) {


        ServiceResult<List<DMSDeviceAlarm>> wResult = new ServiceResult<List<DMSDeviceAlarm>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            List<DMSDeviceAlarm> wServerRst = DMSDeviceAlarmDAO.getInstance().DMS_CurrentDeviceAlarmList(wLoginUser, StringUtils.parseListArgs(wDeviceID), wDeviceNo, "",
                    StringUtils.parseListArgs(wAssetNo), wDeviceType, wModelID, wFactoryID, wWorkShopID, wAreaID, wAlarmCode,
                    wDataGrad, wPositionID, CalendarTool.getBasicDate(), CalendarTool.getBasicDate(), Pagination.getMaxSize(DMSDeviceCurrentAlarm.class), wErrorCode);
            wResult.setResult(wServerRst);
            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_CurrentDeviceAlarmList", ex);
        }
        return wResult;


    }

    @Override
    public ServiceResult<List<DMSDeviceAlarm>> DMS_CurrentDeviceAlarmList(BMSEmployee wLoginUser, List<Integer> wDeviceIDList,
                                                                          String wDeviceNo, String wDeviceName, List<String> wAssetNoList, int wDeviceType, int wModelID,
                                                                          int wFactoryID, int wWorkShopID, int wAreaID, String wAlarmCode, int wDataGrad, int wPositionID, Calendar wStartTime, Calendar wEndTime, Pagination wPagination) {
        ServiceResult<List<DMSDeviceAlarm>> wResult = new ServiceResult<List<DMSDeviceAlarm>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            List<DMSDeviceAlarm> wServerRst = DMSDeviceAlarmDAO.getInstance().DMS_CurrentDeviceAlarmList(wLoginUser,
                    wDeviceIDList, wDeviceNo, wDeviceName, wAssetNoList, wDeviceType, wModelID, wFactoryID, wWorkShopID,
                    wAreaID, wAlarmCode, wDataGrad, wPositionID, wStartTime, wEndTime, wPagination, wErrorCode);
            wResult.setResult(wServerRst);
            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_CurrentDeviceAlarmList", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSDeviceAlarm>> DMS_SelectDeviceAlarmList(BMSEmployee wLoginUser, String wName,
                                                                         String wAssetNo, int wDeviceType, int wModelID, int wFactoryID, int wWorkShopID, int wAreaID,
                                                                         String wAlarmCode, int wEventType, int wDataGrad, int wPositionID, Calendar wStartTime, Calendar wEndTime, Pagination wPagination) {

        ServiceResult<List<DMSDeviceAlarm>> wResult = new ServiceResult<List<DMSDeviceAlarm>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            List<DMSDeviceAlarm> wServerRst = DMSDeviceAlarmDAO.getInstance().DMS_SelectDeviceAlarmList(wLoginUser, wName,
                    wAssetNo, wDeviceType, wModelID, wFactoryID, wWorkShopID, wAreaID,
                    wAlarmCode, wEventType, wDataGrad, wPositionID, wStartTime, wEndTime, wPagination, wErrorCode);
            wResult.setResult(wServerRst);

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceAlarmList", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSDeviceAlarm>> DMS_SelectDeviceAlarm(BMSEmployee wLoginUser, int wID, String wCode,
                                                                     String wAssetNo, String wAlarmCode, int wEventType, int wDataGrad, int wPositionID, Calendar wStartTime, Calendar wEndTime, Pagination wPagination) {
        ServiceResult<List<DMSDeviceAlarm>> wResult = new ServiceResult<List<DMSDeviceAlarm>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            List<DMSDeviceAlarm> wServerRst = DMSDeviceAlarmDAO.getInstance().DMS_SelectDeviceAlarm(wLoginUser, wID,
                    wCode, wAssetNo, wAlarmCode, wEventType, wDataGrad, wPositionID, wStartTime, wEndTime, wPagination, wErrorCode);
            wResult.setResult(wServerRst);

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceAlarm", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSDeviceAlarm>> DMS_SelectDeviceAlarmList(BMSEmployee wLoginUser,
                                                                         List<Integer> wIDList, String wAlarmCode, int wEventType, int wDataGrad, int wPositionID, Calendar wStartTime, Calendar wEndTime, Pagination wPagination) {

        ServiceResult<List<DMSDeviceAlarm>> wResult = new ServiceResult<List<DMSDeviceAlarm>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            List<DMSDeviceAlarm> wServerRst = DMSDeviceAlarmDAO.getInstance().DMS_SelectDeviceAlarmList(wLoginUser, wIDList,
                    wAlarmCode, wEventType, wDataGrad, wPositionID, wStartTime, wEndTime, wPagination, wErrorCode);
            wResult.setResult(wServerRst);

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceAlarmList", ex);
        }
        return wResult;

    }

    @Override
    public ServiceResult<Map<Integer, Map<String, Object>>> DMS_SelectDeviceRealParameterStructList(BMSEmployee wLoginUser, String wName, List<String> wVariableName,

                                                                                                    int wAreaID, int wDeviceID, String wAssetNo, int wDataType, int wDataClass, int wPositionID) {
        ServiceResult<Map<Integer, Map<String, Object>>> wResult = new ServiceResult<Map<Integer, Map<String, Object>>>();
        try {
            Map<Integer, Map<String, Object>> wDeviceDicList = new HashMap<Integer, Map<String, Object>>();
            wResult.Result = wDeviceDicList;

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            List<DMSDeviceRealParameter> wServerRst = DMSDeviceRealParameterDAO.getInstance().DMS_SelectDeviceRealParameterList(wLoginUser,
                    wName, wVariableName, wAreaID, wDeviceID, wAssetNo, wDataType, wDataClass, wPositionID, wErrorCode);


            wResult.AddFaultCode(wErrorCode);

            if (wErrorCode.Result != 0) {
                return wResult;
            }
            for (DMSDeviceRealParameter wDeviceRealParameter : wServerRst) {
                if (!wDeviceDicList.containsKey(wDeviceRealParameter.DeviceID)) {
                    wDeviceDicList.put(wDeviceRealParameter.DeviceID, new HashMap<String, Object>());
                }

                if (!wDeviceDicList.get(wDeviceRealParameter.DeviceID).containsKey(wDeviceRealParameter.VariableName)) {
                    wDeviceDicList.get(wDeviceRealParameter.DeviceID).put(wDeviceRealParameter.VariableName, wDeviceRealParameter.ParameterValue);
                }
            }

        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceRealParameterStructList", ex);
        }
        return wResult;

    }

    @Override
    public ServiceResult<Map<String, Object>> DMS_SelectDeviceCurrentStruct(BMSEmployee wLoginUser, int wDeviceID, String wDeviceNo, String wAssetNo) {
        ServiceResult<Map<String, Object>> wResult = new ServiceResult<Map<String, Object>>();
        try {
            wResult.Result = new HashMap<String, Object>();

            if (wDeviceID <= 0 && StringUtils.isEmpty(wDeviceNo) && StringUtils.isEmpty(wAssetNo)) {
                return wResult;
            }

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);


            //获取对应设备状态
            DMSDeviceStatus wDeviceStatus = DMSDeviceStatusDAO.getInstance().DMS_CurrentDeviceStatus(wLoginUser, wDeviceID, wDeviceNo,
                    wAssetNo, wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
            List<DMSDeviceParameter> wDeviceParameterList = DMSDeviceParameterDAO.getInstance().DMS_SelectDeviceParameterList(wLoginUser,
                    wDeviceID, wDeviceNo, wAssetNo, (int) DMSDataClass.Status.getValue(), Pagination.getMaxSize(), wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
            for (DMSDeviceParameter wDeviceParameter : wDeviceParameterList) {
                if (wDeviceParameter == null || StringUtils.isEmpty(wDeviceParameter.VariableName))
                    continue;
                if (wDeviceParameter.AnalysisOrder <= 0)
                    wDeviceParameter.AnalysisOrder = 1;
                if (wDeviceParameter.AnalysisOrder > 30)
                    wDeviceParameter.AnalysisOrder = 30;
                if (!wResult.Result.containsKey(wDeviceParameter.VariableName)) {
                    wResult.Result.put(wDeviceParameter.VariableName, (wDeviceStatus.Status & (1 << (wDeviceParameter.AnalysisOrder - 1))) > 0 ? 1 : 0);
                }
            }


            //获取对应设备报警
            List<DMSDeviceAlarm> wDeviceAlarmList = DMSDeviceAlarmDAO.getInstance().DMS_CurrentDeviceAlarmList(wLoginUser, wDeviceID, wDeviceNo, wAssetNo, wErrorCode);


            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
            for (DMSDeviceAlarm wDMSDeviceAlarm : wDeviceAlarmList) {
                if (!wResult.Result.containsKey(wDMSDeviceAlarm.AlarmVariableName)) {
                    wResult.Result.put(wDMSDeviceAlarm.AlarmVariableName, 1);
                }
            }

            List<DMSDeviceRealParameter> wDeviceRealParameterList = DMSDeviceRealParameterDAO.getInstance().DMS_SelectDeviceRealParameterList(wLoginUser,
                    "", null, -1, wDeviceID, wAssetNo, -1, -1, -1, wErrorCode);


            wResult.AddFaultCode(wErrorCode);

            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
            for (DMSDeviceRealParameter wDeviceRealParameter : wDeviceRealParameterList) {
                if (!wResult.Result.containsKey(wDeviceRealParameter.VariableName)) {
                    wResult.Result.put(wDeviceRealParameter.VariableName, wDeviceRealParameter.ParameterValue);
                }
            }

        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceCurrentStruct", ex);
        }
        return wResult;


    }

    @Override
    public ServiceResult<List<DMSDeviceRealParameter>> DMS_SelectDeviceRealParameterList(BMSEmployee wLoginUser, String wName, List<String> wVariableName,

                                                                                         int wAreaID, int wDeviceID, String wAssetNo, int wDataType, int wDataClass, int wPositionID) {
        ServiceResult<List<DMSDeviceRealParameter>> wResult = new ServiceResult<List<DMSDeviceRealParameter>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            List<DMSDeviceRealParameter> wServerRst = DMSDeviceRealParameterDAO.getInstance().DMS_SelectDeviceRealParameterList(wLoginUser,
                    wName, wVariableName, wAreaID, wDeviceID, wAssetNo, wDataType, wDataClass, wPositionID, wErrorCode);
            wResult.setResult(wServerRst);
            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceRealParameterList", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSDeviceRealParameter>> DMS_SelectDeviceRealParameterListByDeviceID(BMSEmployee wLoginUser, List<Integer> wIDList) {
        ServiceResult<List<DMSDeviceRealParameter>> wResult = new ServiceResult<List<DMSDeviceRealParameter>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            List<DMSDeviceRealParameter> wServerRst = DMSDeviceRealParameterDAO.getInstance().DMS_SelectDeviceRealParameterListByDeviceID(wLoginUser,
                    wIDList, wErrorCode);
            wResult.setResult(wServerRst);
            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceRealParameterList", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSDeviceRealParameter>> DMS_SelectDeviceRealParameterList(BMSEmployee wLoginUser, List<Integer> wIDList) {
        ServiceResult<List<DMSDeviceRealParameter>> wResult = new ServiceResult<List<DMSDeviceRealParameter>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            List<DMSDeviceRealParameter> wServerRst = DMSDeviceRealParameterDAO.getInstance().DMS_SelectDeviceRealParameterList(wLoginUser,
                    wIDList, wErrorCode);
            wResult.setResult(wServerRst);
            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceRealParameterList", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<DMSDeviceRealParameter> DMS_SelectDeviceRealParameter(BMSEmployee wLoginUser, int wID, String wCode) {
        ServiceResult<DMSDeviceRealParameter> wResult = new ServiceResult<DMSDeviceRealParameter>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wResult.Result = DMSDeviceRealParameterDAO.getInstance().DMS_SelectDeviceRealParameter(wLoginUser,
                    wID, wCode, wErrorCode);
            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceRealParameter", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSDeviceAlarmStatistics>> DMS_SelectDeviceAlarmStatisticsList(BMSEmployee wLoginUser,
                                                                                             int wDeviceID, String wDeviceNo, String wDeviceName, String wAssetNo, int wDeviceType, int wModelID, int wFactoryID,
                                                                                             int wWorkShopID, int wAreaID, int wStatus, Calendar wStartTime, Calendar wEndTime, Pagination wPagination) {
        ServiceResult<List<DMSDeviceAlarmStatistics>> wResult = new ServiceResult<List<DMSDeviceAlarmStatistics>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            List<DMSDeviceStatus> wDMSDeviceStatusList = DMSDeviceStatusDAO.getInstance().DMS_CurrentDeviceStatusList(wLoginUser,
                    StringUtils.parseListArgs(wDeviceID), wDeviceNo, wDeviceName, StringUtils.parseListArgs(wAssetNo), wDeviceType,
                    wModelID, wFactoryID, wWorkShopID, wAreaID, wStatus, CalendarTool.getBasicDate(), CalendarTool.getBasicDate(), wPagination, wErrorCode);


            List<Integer> wDeviceIDList = wDMSDeviceStatusList.stream().map(p -> p.DeviceID).distinct().collect(Collectors.toList());


            wResult.Result = DMSDeviceAlarmDAO.getInstance().DMS_SelectDeviceAlarmStatisticsList(wLoginUser, wDeviceIDList, StatTypes.Day, wStartTime, wEndTime, wErrorCode);

            wResult.AddFaultCode(wErrorCode);
            wResult.Put("Frequency", DMSDeviceAlarmDAO.getInstance().DMS_SelectDeviceAlarmFrequencyList(wLoginUser, wDeviceIDList, wStartTime, wEndTime, wErrorCode));

            wResult.AddFaultCode(wErrorCode);

        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceAlarmStatisticsList", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSDeviceStatusStatistics>> DMS_SelectDeviceStatusStatisticsList(BMSEmployee wLoginUser,
                                                                                               int wDeviceID, String wDeviceNo, String wDeviceName, String wAssetNo, int wDeviceType, int wModelID, int wFactoryID,
                                                                                               int wWorkShopID, int wAreaID, int wStatus, Boolean wIsCombine, Calendar wStartTime, Calendar wEndTime, Pagination wPagination, int wHasAlarm) {
        ServiceResult<List<DMSDeviceStatusStatistics>> wResult = new ServiceResult<List<DMSDeviceStatusStatistics>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wResult.Result = DMSDeviceStatusStatisticsDAO.getInstance().DMS_SelectDeviceStatusStatisticsList(wLoginUser, wDeviceID,
                    wDeviceNo, wDeviceName,
                    wAssetNo, wDeviceType, wModelID, wFactoryID, wWorkShopID, wAreaID, wStatus, wIsCombine,
                    wStartTime, wEndTime, wPagination, wErrorCode);

            wResult.AddFaultCode(wErrorCode);


            if (wResult.Result == null || wResult.Result.size() <= 0) {
                wResult.Put("Alarm", new ArrayList<DMSDeviceAlarmStatistics>());
                wResult.Put("Frequency", new ArrayList<DMSDeviceAlarmFrequency>());
                return wResult;
            }


            if (wHasAlarm > 0) {
                List<Integer> wDeviceIDList = wResult.Result.stream().map(p -> p.DeviceID).distinct().collect(Collectors.toList());
                wResult.Put("Alarm", DMSDeviceAlarmDAO.getInstance().DMS_SelectDeviceAlarmStatisticsList(wLoginUser, wDeviceIDList, StatTypes.Day, wStartTime, wEndTime, wErrorCode));

                wResult.AddFaultCode(wErrorCode);
                wResult.Put("Frequency", DMSDeviceAlarmDAO.getInstance().DMS_SelectDeviceAlarmFrequencyList(wLoginUser, wDeviceIDList, wStartTime, wEndTime, wErrorCode));

                wResult.AddFaultCode(wErrorCode);
            }


        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectDeviceStatusStatisticsList", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSProcessRecord>> DMS_SelectProcessRecordList(BMSEmployee wLoginUser, int wProductID, int wOrderID,
                                                                             String wOrderNo, String wAssetNo, int wDeviceID, String wDeviceNo, String wWorkpieceNo, int wRecordType, List<Integer> wDataClassList,
                                                                             int wActive, int wStatus, Calendar wStartTime, Calendar wEndTime,
                                                                             int wTechnologyID, Pagination wPagination) {
        ServiceResult<List<DMSProcessRecord>> wResult = new ServiceResult<List<DMSProcessRecord>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            //if(WEB.GlobalContext.ProcessRecordType == 1)
            wResult.Result = DMSProcessRecordDAO.getInstance().DMS_SelectProcessRecordListNew(wLoginUser, wProductID,
                    wOrderID, wOrderNo, wAssetNo, wDeviceID, wDeviceNo, wWorkpieceNo, wRecordType,
                    wDataClassList, wActive, wStatus, -1, wStartTime, wEndTime, wTechnologyID, wPagination, wErrorCode);

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectProcessRecordList", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSProcessRecord>> DMS_SelectProcessRecordUploadList(BMSEmployee wLoginUser) {
        ServiceResult<List<DMSProcessRecord>> wResult = new ServiceResult<List<DMSProcessRecord>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wResult.Result = DMSProcessRecordDAO.getInstance().DMS_SelectProcessRecordUploadList(wLoginUser, Pagination.Create(1, 100), wErrorCode);

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectProcessRecordUploadList", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> DMS_UpdateProcessRecordUploadStatus(BMSEmployee wLoginUser, List<Integer> wRecordIDList, int wUploadStatus) {

        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            DMSProcessRecordDAO.getInstance().DMS_UpdateProcessRecordUploadStatus(wLoginUser, wRecordIDList, wUploadStatus, wErrorCode);

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_UpdateProcessRecordUploadStatus", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<DMSProcessRecord> DMS_SelectLastProcessRecord(BMSEmployee wLoginUser, String wWorkpieceNo, int wRecordType) {
        ServiceResult<DMSProcessRecord> wResult = new ServiceResult<DMSProcessRecord>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wResult.Result = DMSProcessRecordDAO.getInstance().DMS_SelectLastRecord(wLoginUser, wWorkpieceNo, wRecordType
                    , wErrorCode);
            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectLastProcessRecord", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<DMSProcessRecord> DMS_SelectProcessRecord(BMSEmployee wLoginUser, int wID) {
        ServiceResult<DMSProcessRecord> wResult = new ServiceResult<DMSProcessRecord>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wResult.Result = DMSProcessRecordDAO.getInstance().DMS_SelectProcessRecord(wLoginUser,
                    wID, wErrorCode);
            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectProcessRecord", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSProcessRecordItem>> DMS_SelectProcessRecordItemList(BMSEmployee wLoginUser, int wRecordID) {

        ServiceResult<List<DMSProcessRecordItem>> wResult = new ServiceResult<List<DMSProcessRecordItem>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wResult.Result = DMSProcessRecordDAO.getInstance().DMS_SelectProcessRecordItemList(wLoginUser, wRecordID, wErrorCode);

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectProcessRecordItemList", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSProcessRecordItem>> DMS_SelectProcessRecordItemList(BMSEmployee wLoginUser, int wDeviceID, String wDeviceNo, int wRecordID,
                                                                                     int wParameterID, String wParameterNo, int wActive, int wStatus, Calendar wStartTime, Calendar wEndTime, Pagination wPagination) {

        ServiceResult<List<DMSProcessRecordItem>> wResult = new ServiceResult<List<DMSProcessRecordItem>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            wResult.Result = DMSProcessRecordDAO.getInstance().DMS_SelectProcessRecordItemList(wLoginUser, wDeviceID, wDeviceNo, wRecordID,
                    wParameterID, wParameterNo, wActive, wStatus, wStartTime, wEndTime, wPagination, wErrorCode);

            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectProcessRecordItemList", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> DMS_SyncDeviceStatus(BMSEmployee wLoginUser, String wAssetNo, int wStatus) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        List<String> wSqlStringList = new ArrayList<String>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            if (StringUtils.isEmpty(wAssetNo))
                return wResult;
            DMSDeviceStatus wDMSDeviceStatus = DMSDeviceStatusDAO.getInstance().DMS_CurrentDeviceStatus(wLoginUser, -1, "", wAssetNo, wErrorCode);

            if (wDMSDeviceStatus.DeviceID <= 0) {
                return wResult;
            }

            List<DMSDeviceStatus> wCurrentDeviceStatusDetailList = DMSDeviceStatusDAO.getInstance().DMS_SelectDeviceCurrentStatusDetailList(wLoginUser, StringUtils.parseListArgs(wDMSDeviceStatus.DeviceID), wErrorCode);

            Map<Integer, DMSDeviceStatus> wCurrentDeviceStatusDetailDic = wCurrentDeviceStatusDetailList.stream().collect(Collectors.toMap(p -> p.Status, p -> p,
                    (o1, o2) -> o1.StatusTime.compareTo(o2.StatusTime) > 0 ? o1 : o2));


            boolean wIsCurrent = false;


            List<DMSDeviceStatus> wCloseDetailStatusList = new ArrayList<DMSDeviceStatus>();


            //region 对比数据库状态是否缺少某些详情状态 则补充到列表

            Map<String, List<Integer>> wCompareStatus = DMSDeviceStatusEnumTool.getInstance().CompareStatus(DMSDeviceStatusEnumTool.getInstance().ListToStatus(wCurrentDeviceStatusDetailDic.keySet()),
                    wDMSDeviceStatus.Status);

            List<Integer> wKeepStatus = wCompareStatus.get("Result");
            List<Integer> wLiveStatus = wCompareStatus.get("Live");
            List<Integer> wOverStatus = wCompareStatus.get("Over");

            //对比原本状态是否缺少某些状态 则补充到列表

            if (wLiveStatus.size() > 0) {
                //补充
                for (int item : wLiveStatus) {
                    if (wCurrentDeviceStatusDetailDic.containsKey(item))
                        continue;
                    wCurrentDeviceStatusDetailDic.put(item, wDMSDeviceStatus.Clone());

                    wCurrentDeviceStatusDetailDic.get(item).ID = 0;
                    wCurrentDeviceStatusDetailDic.get(item).Status = item;
                }
            }
            if (wOverStatus.size() > 0) {
                //关闭
                wCloseDetailStatusList.clear();
                for (int item : wOverStatus) {
                    if (wCurrentDeviceStatusDetailDic.containsKey(item)) {
                        wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd = Calendar.getInstance();
                        wCurrentDeviceStatusDetailDic.get(item).Duration = (int) (CalendarTool.SubSeconds(wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd
                                , wCurrentDeviceStatusDetailDic.get(item).StatusTime));
                        wCloseDetailStatusList.add(wCurrentDeviceStatusDetailDic.get(item));
                        wCurrentDeviceStatusDetailDic.remove(item);
                    }
                }
                //加入插入或更新列表
                wSqlStringList.addAll(DMSDeviceStatusDAO.getInstance().DMS_CloseDetailStatusString(wCloseDetailStatusList, wDMSDeviceStatus.StatusTimeEnd));

            }
            if (wKeepStatus.size() > 0) {
                for (int item : wKeepStatus) {
                    if (!wCurrentDeviceStatusDetailDic.containsKey(item)) {

                        wCurrentDeviceStatusDetailDic.put(item, wDMSDeviceStatus.Clone());
                        wCurrentDeviceStatusDetailDic.get(item).ID = 0;
                        wCurrentDeviceStatusDetailDic.get(item).Status = item;
                        continue;
                    }
                    wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd = Calendar.getInstance();
                    wCurrentDeviceStatusDetailDic.get(item).Duration = (int) (CalendarTool.SubSeconds(wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd
                            , wCurrentDeviceStatusDetailDic.get(item).StatusTime));
                }
            }

            //endregion


            if (wStatus == wDMSDeviceStatus.Status) {
                wDMSDeviceStatus.Duration = (int) (CalendarTool.SubSeconds(Calendar.getInstance()
                        , wDMSDeviceStatus.StatusTime));
                wDMSDeviceStatus.StatusTimeEnd = Calendar.getInstance();
                wIsCurrent = true;


            } else {

                wDMSDeviceStatus.Duration = (int) (CalendarTool.SubSeconds(Calendar.getInstance()
                        , wDMSDeviceStatus.StatusTime));
                wDMSDeviceStatus.StatusTimeEnd = Calendar.getInstance();

                //历史记录更改 将Active=1 and DeviceID   的EndDate以及Duration和Active更新
                wSqlStringList.addAll(DMSDeviceStatusDAO.getInstance().DMS_UpdateActiveStatusString(wDMSDeviceStatus.AssetNo, wDMSDeviceStatus.StatusTimeEnd, wDMSDeviceStatus.Status));

                //若Active=1没有则新增一条记录 若Active=1存在但状态不对 先将Active改为0 然后新增一条记录
                //不管历史 跳过此不走


                //region 对当前状态变更 少了什么状态  多了什么状态 与详情状态对比，
                wCompareStatus = DMSDeviceStatusEnumTool.getInstance().CompareStatus(DMSDeviceStatusEnumTool.getInstance().ListToStatus(wCurrentDeviceStatusDetailDic.keySet()),
                        wStatus);

                wKeepStatus = wCompareStatus.get("Result");
                wLiveStatus = wCompareStatus.get("Live");
                wOverStatus = wCompareStatus.get("Over");

                //增加了哪些
                //新增对应详情状态
                if (wLiveStatus.size() > 0) {
                    //补充
                    for (int item : wLiveStatus) {
                        if (wCurrentDeviceStatusDetailDic.containsKey(item))
                            continue;
                        wCurrentDeviceStatusDetailDic.put(item, wDMSDeviceStatus.Clone());
                        wCurrentDeviceStatusDetailDic.get(item).StatusTime = Calendar.getInstance();
                        wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd = Calendar.getInstance();
                        wCurrentDeviceStatusDetailDic.get(item).Duration = 0;
                        wCurrentDeviceStatusDetailDic.get(item).Status = item;
                        wCurrentDeviceStatusDetailDic.get(item).ID = 0;
                    }
                }
                //消失了哪些
                //关闭对应详情状态
                if (wOverStatus.size() > 0) {
                    //关闭
                    wCloseDetailStatusList.clear();
                    for (int item : wOverStatus) {
                        if (wCurrentDeviceStatusDetailDic.containsKey(item)) {
                            wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd = Calendar.getInstance();
                            wCurrentDeviceStatusDetailDic.get(item).Duration =
                                    // ((int) wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd.Subtract(wCurrentDeviceStatusDetailDic.get(item).StatusTime).TotalSeconds);
                                    (int) (CalendarTool.SubSeconds(wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd
                                            , wCurrentDeviceStatusDetailDic.get(item).StatusTime));
                            wCloseDetailStatusList.add(wCurrentDeviceStatusDetailDic.get(item));
                            wCurrentDeviceStatusDetailDic.remove(item);
                        }
                    }
                    //加入插入或更新列表
                    wSqlStringList.addAll(DMSDeviceStatusDAO.getInstance().DMS_CloseDetailStatusString(wCloseDetailStatusList, wDMSDeviceStatus.StatusTimeEnd));
                }

                if (wKeepStatus.size() > 0) {
                    for (int item : wKeepStatus) {
                        if (!wCurrentDeviceStatusDetailDic.containsKey(item)) {

                            wCurrentDeviceStatusDetailDic.put(item, wDMSDeviceStatus.Clone());
                            wCurrentDeviceStatusDetailDic.get(item).ID = 0;
                            wCurrentDeviceStatusDetailDic.get(item).Status = item;
                            continue;
                        }
                        wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd = Calendar.getInstance();
                        wCurrentDeviceStatusDetailDic.get(item).Duration =

                                (int) (CalendarTool.SubSeconds(wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd
                                        , wCurrentDeviceStatusDetailDic.get(item).StatusTime));
                    }
                }

                //endregion

                //不消失一直存在的
                //更新持续时间

                //修改当前状态 wDMSDeviceStatus  StatusTime 以及Status Duration StatusTimeEnd
                wDMSDeviceStatus.StatusHistory = wDMSDeviceStatus.Status;
                wDMSDeviceStatus.Status = wStatus;
                wDMSDeviceStatus.StatusTime = Calendar.getInstance();
                wDMSDeviceStatus.StatusTimeEnd = Calendar.getInstance();
                //然后重新插入一条这个当前状态数据
                wSqlStringList.add(DMSDeviceStatusDAO.getInstance().DMS_InsertActiveStatusString(wDMSDeviceStatus));

                wIsCurrent = false;


            }
            if (wIsCurrent) {
                //更新当前历史状态时长
                wSqlStringList.add(DMSDeviceStatusDAO.getInstance().DMS_KeepActiveStatusString(wDMSDeviceStatus));

            }
            //更新当前历史详情状态时长  用最后一个时间来更新
            wSqlStringList.addAll(DMSDeviceStatusDAO.getInstance().DMS_KeepDetailStatusString(wCurrentDeviceStatusDetailDic.values(), wDMSDeviceStatus.StatusTimeEnd));

            //更新或插入当前设备状态
            wSqlStringList.add(DMSDeviceStatusDAO.getInstance().DMS_UpdateCurrentStatusString(wDMSDeviceStatus));


            DMSDeviceStatusDAO.getInstance().ExecuteSqlTransaction(wSqlStringList);


        } catch (Exception e) {
            wResult.FaultCode += e.getMessage();
            log.error("DMS_SyncDeviceStatus", e);

            log.info(StringUtils.Join("\n", wSqlStringList));
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<String>> DMS_SyncDeviceStatusList(BMSEmployee wLoginUser, List<DMSDeviceStatus> wDMSDeviceStatusList) {
        ServiceResult<List<String>> wResult = new ServiceResult<List<String>>();
        try {
            wResult.Result = new ArrayList<String>();
            if (wDMSDeviceStatusList == null || wDMSDeviceStatusList.size() <= 0)
                return wResult;

            wDMSDeviceStatusList.sort((o1, o2) -> o1.StatusTime.compareTo(o2.StatusTime));

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            List<String> wAssetNoList = wDMSDeviceStatusList.stream().map(p -> p.AssetNo).distinct().collect(Collectors.toList());

            Map<String, List<DMSDeviceStatus>> wDMSDeviceStatusListDic = wDMSDeviceStatusList.stream().collect(Collectors.groupingBy(p -> p.AssetNo));

            //数据库当前状态与数据当前状态对比
            //有变更则查询最后一条历史记录
            //与数据库当前状态对比 更新结束时间并添加变更历史记录
            //数据库当前状态变更为当前状态
            List<DMSDeviceStatus> wCurrentDeviceStatusList = DMSDeviceStatusDAO.getInstance().DMS_CurrentDeviceStatusList(wLoginUser, null, wAssetNoList, wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
            List<Integer> wDeviceIDList = wCurrentDeviceStatusList.stream().map(p -> p.DeviceID).distinct().collect(Collectors.toList());
            wDeviceIDList.removeIf(p -> p <= 0);

            List<DMSDeviceStatus> wCurrentDeviceStatusDetailSourceList = DMSDeviceStatusDAO.getInstance().DMS_SelectDeviceCurrentStatusDetailList(wLoginUser, wDeviceIDList, wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
            Map<String, List<DMSDeviceStatus>> wCurrentDeviceStatusDetailListDic = wCurrentDeviceStatusDetailSourceList.stream().collect(Collectors.groupingBy(p -> p.AssetNo));

            Map<Integer, DMSDeviceStatus> wCurrentDeviceStatusDetailDic = null;

            boolean wIsCurrent = false;

            List<Integer> wKeepStatus = null;

            List<Integer> wLiveStatus = null;

            List<Integer> wOverStatus = null;

            Map<String, List<Integer>> wCompareStatus = null;

            List<String> wSqlStringList = new ArrayList<String>();

            List<DMSDeviceStatus> wCloseDetailStatusList = new ArrayList<DMSDeviceStatus>();

            Calendar wMaxStatusTime = Calendar.getInstance();
            for (DMSDeviceStatus wDMSDeviceStatus : wCurrentDeviceStatusList) {
                if (!wDMSDeviceStatusListDic.containsKey(wDMSDeviceStatus.AssetNo)) //无此设备
                {
                    wResult.Result.add(StringUtils.Format("AssetNo:{0} Not Found!", wDMSDeviceStatus.AssetNo));
                    continue;
                }
                wMaxStatusTime = wDMSDeviceStatusListDic.get(wDMSDeviceStatus.AssetNo).stream().max(Comparator.comparing(DMSDeviceStatus::getStatusTime)).get().StatusTime;

                if (wMaxStatusTime.compareTo(wDMSDeviceStatus.StatusTime) < 0) {
                    wResult.Result.add(StringUtils.Format("AssetNo:{0} MaxStatusTime:{1} CurrentStatusTime:{2} Data too old!",
                            wDMSDeviceStatus.AssetNo, StringUtils.parseCalendarToString(wMaxStatusTime, "yyyy-MM-dd HH:mm:ss"),
                            StringUtils.parseCalendarToString(wDMSDeviceStatus.StatusTime, "yyyy-MM-dd HH:mm:ss")));
                    continue;
                }
                wDMSDeviceStatusListDic.get(wDMSDeviceStatus.AssetNo).removeIf(p -> p.StatusTime.compareTo(wDMSDeviceStatus.StatusTime) <= 0);

                if (wDMSDeviceStatusListDic.get(wDMSDeviceStatus.AssetNo).size() <= 0)
                    continue;

                if (!wCurrentDeviceStatusDetailListDic.containsKey(wDMSDeviceStatus.AssetNo))
                    wCurrentDeviceStatusDetailListDic.put(wDMSDeviceStatus.AssetNo, new ArrayList<DMSDeviceStatus>());

                wCurrentDeviceStatusDetailDic = wCurrentDeviceStatusDetailListDic.get(wDMSDeviceStatus.AssetNo).stream().collect(Collectors.toMap(p -> p.Status, p -> p, (o1, o2) -> o1.StatusTime.compareTo(o2.StatusTime) > 0 ? o1 : o2));

                //所有记录全部插入


                wIsCurrent = false;
                //第一条对比当前状态一样则跳过
                for (DMSDeviceStatus wDeviceStatus : wDMSDeviceStatusListDic.get(wDMSDeviceStatus.AssetNo)) {
                    if (wDeviceStatus.Status == wDMSDeviceStatus.Status) {
                        wDMSDeviceStatus.Duration = (int) CalendarTool.SubSeconds(wDeviceStatus.StatusTime, wDMSDeviceStatus.StatusTime);
                        wDMSDeviceStatus.StatusTimeEnd = wDeviceStatus.StatusTime;
                        wIsCurrent = true;
                        continue;
                    }
                    wDMSDeviceStatus.Duration = (int) CalendarTool.SubSeconds(wDeviceStatus.StatusTime, wDMSDeviceStatus.StatusTime);
                    wDMSDeviceStatus.StatusTimeEnd = wDeviceStatus.StatusTime;

                    //历史记录更改 将Active=1 and DeviceID   的EndDate以及Duration和Active更新
                    wSqlStringList.addAll(DMSDeviceStatusDAO.getInstance().DMS_UpdateActiveStatusString(wDMSDeviceStatus.AssetNo, wDMSDeviceStatus.StatusTimeEnd, wDMSDeviceStatus.Status));

                    //若Active=1没有则新增一条记录 若Active=1存在但状态不对 先将Active改为0 然后新增一条记录
                    //不管历史 跳过此不走


                    //region 对比原本状态是否缺少某些状态 则补充到列表
                    wCompareStatus = DMSDeviceStatusEnumTool.getInstance().CompareStatus(DMSDeviceStatusEnumTool.getInstance().ListToStatus(wCurrentDeviceStatusDetailDic.keySet()),
                            wDMSDeviceStatus.Status);
                    wKeepStatus = wCompareStatus.get("Result");
                    wLiveStatus = wCompareStatus.get("Live");
                    wOverStatus = wCompareStatus.get("Over");
                    //对比原本状态是否缺少某些状态 则补充到列表

                    if (wLiveStatus.size() > 0) {
                        //补充
                        for (int item : wLiveStatus) {
                            if (wCurrentDeviceStatusDetailDic.containsKey(item))
                                continue;
                            wCurrentDeviceStatusDetailDic.put(item, wDMSDeviceStatus.Clone());

                            wCurrentDeviceStatusDetailDic.get(item).ID = 0;
                            wCurrentDeviceStatusDetailDic.get(item).Status = item;
                        }
                    }
                    if (wOverStatus.size() > 0) {
                        //关闭
                        wCloseDetailStatusList.clear();
                        for (int item : wOverStatus) {
                            if (wCurrentDeviceStatusDetailDic.containsKey(item)) {
                                wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd = wDeviceStatus.StatusTime;
                                wCurrentDeviceStatusDetailDic.get(item).Duration = (int) CalendarTool.SubSeconds(wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd, wCurrentDeviceStatusDetailDic.get(item).StatusTime);

                                wCloseDetailStatusList.add(wCurrentDeviceStatusDetailDic.get(item));
                                wCurrentDeviceStatusDetailDic.remove(item);
                            }
                        }
                        //加入插入或更新列表
                        wSqlStringList.addAll(DMSDeviceStatusDAO.getInstance().DMS_CloseDetailStatusString(wCloseDetailStatusList, wDMSDeviceStatus.StatusTimeEnd));

                    }

                    //endregion

                    //region 对当前状态变更 少了什么状态  多了什么状态 与详情状态对比，
                    wCompareStatus = DMSDeviceStatusEnumTool.getInstance().CompareStatus(DMSDeviceStatusEnumTool.getInstance().ListToStatus(wCurrentDeviceStatusDetailDic.keySet()),
                            wDeviceStatus.Status);
                    wKeepStatus = wCompareStatus.get("Result");
                    wLiveStatus = wCompareStatus.get("Live");
                    wOverStatus = wCompareStatus.get("Over");
                    //增加了哪些
                    //新增对应详情状态
                    if (wLiveStatus.size() > 0) {
                        //补充
                        for (int item : wLiveStatus) {
                            if (wCurrentDeviceStatusDetailDic.containsKey(item))
                                continue;
                            wCurrentDeviceStatusDetailDic.put(item, wDMSDeviceStatus.Clone());
                            wCurrentDeviceStatusDetailDic.get(item).StatusTime = wDeviceStatus.StatusTime;
                            wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd = wDeviceStatus.StatusTime;
                            wCurrentDeviceStatusDetailDic.get(item).Duration = 0;
                            wCurrentDeviceStatusDetailDic.get(item).Status = item;
                            wCurrentDeviceStatusDetailDic.get(item).ID = 0;
                        }
                    }
                    //消失了哪些
                    //关闭对应详情状态
                    if (wOverStatus.size() > 0) {
                        //关闭
                        wCloseDetailStatusList.clear();
                        for (int item : wOverStatus) {
                            if (wCurrentDeviceStatusDetailDic.containsKey(item)) {
                                wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd = wDeviceStatus.StatusTime;
                                wCurrentDeviceStatusDetailDic.get(item).Duration = (int) CalendarTool.SubSeconds(wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd
                                        , wCurrentDeviceStatusDetailDic.get(item).StatusTime);
                                wCloseDetailStatusList.add(wCurrentDeviceStatusDetailDic.get(item));
                                wCurrentDeviceStatusDetailDic.remove(item);
                            }
                        }
                        //加入插入或更新列表
                        wSqlStringList.addAll(DMSDeviceStatusDAO.getInstance().DMS_CloseDetailStatusString(wCloseDetailStatusList, wDMSDeviceStatus.StatusTimeEnd));

                    }

                    //endregion

                    //不消失一直存在的
                    //更新持续时间

                    //修改当前状态 wDMSDeviceStatus  StatusTime 以及Status Duration StatusTimeEnd
                    wDMSDeviceStatus.StatusHistory = wDMSDeviceStatus.Status;
                    wDMSDeviceStatus.Status = wDeviceStatus.Status;
                    wDMSDeviceStatus.StatusTime = wDeviceStatus.StatusTime;
                    wDMSDeviceStatus.StatusTimeEnd = wDeviceStatus.StatusTimeEnd;
                    //然后重新插入一条这个当前状态数据
                    wSqlStringList.add(DMSDeviceStatusDAO.getInstance().DMS_InsertActiveStatusString(wDMSDeviceStatus));

                    wIsCurrent = false;
                }
                //如果历史状态未变更但时长变更最后需要更新一下历史设备状态时长 包括详情时长 用最后这个状态时间来减去开始时间
                if (wIsCurrent) {
                    //更新当前历史状态时长
                    wSqlStringList.add(DMSDeviceStatusDAO.getInstance().DMS_KeepActiveStatusString(wDMSDeviceStatus));

                }
                //更新当前历史详情状态时长  用最后一个时间来更新
                wSqlStringList.addAll(DMSDeviceStatusDAO.getInstance().DMS_KeepDetailStatusString(wCurrentDeviceStatusDetailDic.values(), wDMSDeviceStatus.StatusTimeEnd));

                //更新或插入当前设备状态
                wSqlStringList.add(DMSDeviceStatusDAO.getInstance().DMS_UpdateCurrentStatusString(wDMSDeviceStatus));

            }

            DMSDeviceStatusDAO.getInstance().ExecuteSqlTransaction(wSqlStringList);
        } catch (Exception e) {
            wResult.FaultCode += e.getMessage();
            log.error("DMS_SyncDeviceStatusList", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<String>> DMS_SyncDeviceStatusHistoryList(BMSEmployee wLoginUser, List<DMSDeviceStatus> wDMSDeviceStatusList) {
        ServiceResult<List<String>> wResult = new ServiceResult<List<String>>();
        try {
            wResult.Result = new ArrayList<String>();
            if (wDMSDeviceStatusList == null || wDMSDeviceStatusList.size() <= 0)
                return wResult;

            wDMSDeviceStatusList.sort((o1, o2) -> o1.StatusTime.compareTo(o2.StatusTime));

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            List<String> wAssetNoList = wDMSDeviceStatusList.stream().map(p -> p.AssetNo).distinct().collect(Collectors.toList());

            Map<String, List<DMSDeviceStatus>> wDMSDeviceStatusListDic = wDMSDeviceStatusList.stream().collect(Collectors.groupingBy(p -> p.AssetNo));

            //数据库当前状态与数据当前状态对比
            //有变更则查询最后一条历史记录
            //与数据库当前状态对比 更新结束时间并添加变更历史记录
            //数据库当前状态变更为当前状态
            List<DMSDeviceStatus> wCurrentDeviceStatusList = DMSDeviceStatusDAO.getInstance().DMS_CurrentDeviceStatusList(wLoginUser, null, wAssetNoList, wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
            List<Integer> wDeviceIDList = wCurrentDeviceStatusList.stream().map(p -> p.DeviceID).distinct().collect(Collectors.toList());
            wDeviceIDList.removeIf(p -> p <= 0);

            List<DMSDeviceStatus> wCurrentDeviceStatusDetailSourceList = DMSDeviceStatusDAO.getInstance().DMS_SelectDeviceCurrentStatusDetailList(wLoginUser, wDeviceIDList, wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
            Map<String, List<DMSDeviceStatus>> wCurrentDeviceStatusDetailListDic = wCurrentDeviceStatusDetailSourceList.stream().collect(Collectors.groupingBy(p -> p.AssetNo));

            Map<Integer, DMSDeviceStatus> wCurrentDeviceStatusDetailDic = null;

            boolean wIsCurrent = false;

            List<Integer> wKeepStatus = null;

            List<Integer> wLiveStatus = null;

            List<Integer> wOverStatus = null;

            Map<String, List<Integer>> wCompareStatus = null;

            List<String> wSqlStringList = new ArrayList<String>();

            List<DMSDeviceStatus> wCloseDetailStatusList = new ArrayList<DMSDeviceStatus>();

            Calendar wMaxStatusTime = Calendar.getInstance();
            for (DMSDeviceStatus wDMSDeviceStatus : wCurrentDeviceStatusList) {
                if (!wDMSDeviceStatusListDic.containsKey(wDMSDeviceStatus.AssetNo)) //无此设备
                {
                    wResult.Result.add(StringUtils.Format("AssetNo:{0} Not Found!", wDMSDeviceStatus.AssetNo));
                    continue;
                }
                wMaxStatusTime = wDMSDeviceStatusListDic.get(wDMSDeviceStatus.AssetNo).stream().max(Comparator.comparing(DMSDeviceStatus::getStatusTime)).get().getStatusTime();

                if (wMaxStatusTime.compareTo(wDMSDeviceStatus.StatusTime) < 0) {
                    wResult.Result.add(StringUtils.Format("AssetNo:{0} MaxStatusTime:{1} CurrentStatusTime:{2} Data too old!",
                            wDMSDeviceStatus.AssetNo, StringUtils.parseCalendarToString(wMaxStatusTime, "yyyy-MM-dd HH:mm:ss"), StringUtils.parseCalendarToString(wDMSDeviceStatus.StatusTime, "yyyy-MM-dd HH:mm:ss")));
                    continue;
                }
                wDMSDeviceStatusListDic.get(wDMSDeviceStatus.AssetNo).removeIf(p -> p.StatusTime.compareTo(wDMSDeviceStatus.StatusTime) <= 0);

                if (wDMSDeviceStatusListDic.get(wDMSDeviceStatus.AssetNo).size() == 0)
                    continue;

                if (!wCurrentDeviceStatusDetailListDic.containsKey(wDMSDeviceStatus.AssetNo))
                    wCurrentDeviceStatusDetailListDic.put(wDMSDeviceStatus.AssetNo, new ArrayList<DMSDeviceStatus>());

                wCurrentDeviceStatusDetailDic = wCurrentDeviceStatusDetailListDic.get(wDMSDeviceStatus.AssetNo)
                        .stream().collect(Collectors.toMap(q -> q.Status, q -> q, (o1, o2) -> o1.StatusTime.compareTo(o2.StatusTime) > 0 ? o1 : o2));

                //所有记录全部插入


                wIsCurrent = false;
                //第一条对比当前状态一样则跳过
                for (DMSDeviceStatus wDeviceStatus : wDMSDeviceStatusListDic.get(wDMSDeviceStatus.AssetNo)) {
                    if (wDeviceStatus.Status == wDMSDeviceStatus.Status) {
                        wDMSDeviceStatus.Duration = (int) CalendarTool.SubSeconds(wDeviceStatus.StatusTime, wDMSDeviceStatus.StatusTime);
                        wDMSDeviceStatus.StatusTimeEnd = wDeviceStatus.StatusTime;
                        wIsCurrent = true;
                        continue;
                    }
                    wDMSDeviceStatus.Duration = (int) CalendarTool.SubSeconds(wDeviceStatus.StatusTime, wDMSDeviceStatus.StatusTime);
                    wDMSDeviceStatus.StatusTimeEnd = wDeviceStatus.StatusTime;

                    //历史记录更改 将Active=1 and DeviceID   的EndDate以及Duration和Active更新
                    wSqlStringList.addAll(DMSDeviceStatusDAO.getInstance().DMS_UpdateActiveStatusString(wDMSDeviceStatus.AssetNo, wDMSDeviceStatus.StatusTimeEnd, wDMSDeviceStatus.Status));

                    //若Active=1没有则新增一条记录 若Active=1存在但状态不对 先将Active改为0 然后新增一条记录
                    //不管历史 跳过此不走


                    //region 对比原本状态是否缺少某些状态 则补充到列表
                    wCompareStatus = DMSDeviceStatusEnumTool.getInstance().CompareStatus(DMSDeviceStatusEnumTool.getInstance().ListToStatus(wCurrentDeviceStatusDetailDic.keySet()),
                            wDMSDeviceStatus.Status);
                    wKeepStatus = wCompareStatus.get("Result");
                    wLiveStatus = wCompareStatus.get("Live");
                    wOverStatus = wCompareStatus.get("Over");
                    //对比原本状态是否缺少某些状态 则补充到列表

                    if (wLiveStatus.size() > 0) {
                        //补充
                        for (int item : wLiveStatus) {
                            if (wCurrentDeviceStatusDetailDic.containsKey(item))
                                continue;
                            wCurrentDeviceStatusDetailDic.put(item, wDMSDeviceStatus.Clone());

                            wCurrentDeviceStatusDetailDic.get(item).Status = item;
                        }
                    }
                    if (wOverStatus.size() > 0) {
                        //关闭
                        wCloseDetailStatusList.clear();
                        for (int item : wOverStatus) {
                            if (wCurrentDeviceStatusDetailDic.containsKey(item)) {
                                wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd = wDeviceStatus.StatusTime;
                                wCurrentDeviceStatusDetailDic.get(item).Duration = (int) CalendarTool.SubSeconds(wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd
                                        , wCurrentDeviceStatusDetailDic.get(item).StatusTime);
                                wCloseDetailStatusList.add(wCurrentDeviceStatusDetailDic.get(item));
                                wCurrentDeviceStatusDetailDic.remove(item);
                            }
                        }
                        //加入插入或更新列表
                        wSqlStringList.addAll(DMSDeviceStatusDAO.getInstance().DMS_CloseDetailStatusString(wCloseDetailStatusList, wDMSDeviceStatus.StatusTimeEnd));

                    }

                    //endregion

                    //region 对当前状态变更 少了什么状态  多了什么状态 与详情状态对比，
                    wCompareStatus = DMSDeviceStatusEnumTool.getInstance().CompareStatus(DMSDeviceStatusEnumTool.getInstance().ListToStatus(wCurrentDeviceStatusDetailDic.keySet()),
                            wDeviceStatus.Status);
                    wKeepStatus = wCompareStatus.get("Result");
                    wLiveStatus = wCompareStatus.get("Live");
                    wOverStatus = wCompareStatus.get("Over");
                    //增加了哪些
                    //新增对应详情状态
                    if (wLiveStatus.size() > 0) {
                        //补充
                        for (int item : wLiveStatus) {
                            if (wCurrentDeviceStatusDetailDic.containsKey(item))
                                continue;
                            wCurrentDeviceStatusDetailDic.put(item, wDMSDeviceStatus.Clone());
                            wCurrentDeviceStatusDetailDic.get(item).StatusTime = wDeviceStatus.StatusTime;
                            wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd = wDeviceStatus.StatusTime;
                            wCurrentDeviceStatusDetailDic.get(item).Duration = 0;
                            wCurrentDeviceStatusDetailDic.get(item).Status = item;
                        }
                    }
                    //消失了哪些
                    //关闭对应详情状态
                    if (wOverStatus.size() > 0) {
                        //关闭
                        wCloseDetailStatusList.clear();
                        for (Integer item : wOverStatus) {
                            if (wCurrentDeviceStatusDetailDic.containsKey(item)) {
                                wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd = wDeviceStatus.StatusTime;
                                wCurrentDeviceStatusDetailDic.get(item).Duration = (int) CalendarTool.SubSeconds(wCurrentDeviceStatusDetailDic.get(item).StatusTimeEnd
                                        , wCurrentDeviceStatusDetailDic.get(item).StatusTime);
                                wCloseDetailStatusList.add(wCurrentDeviceStatusDetailDic.get(item));
                                wCurrentDeviceStatusDetailDic.remove(item);
                            }
                        }
                        //加入插入或更新列表
                        wSqlStringList.addAll(DMSDeviceStatusDAO.getInstance().DMS_CloseDetailStatusString(wCloseDetailStatusList, wDMSDeviceStatus.StatusTimeEnd));

                    }

                    //endregion

                    //不消失一直存在的
                    //更新持续时间

                    //修改当前状态 wDMSDeviceStatus  StatusTime 以及Status Duration StatusTimeEnd
                    wDMSDeviceStatus.StatusHistory = wDMSDeviceStatus.Status;
                    wDMSDeviceStatus.Status = wDeviceStatus.Status;
                    wDMSDeviceStatus.StatusTime = wDeviceStatus.StatusTime;
                    wDMSDeviceStatus.StatusTimeEnd = wDeviceStatus.StatusTimeEnd;
                    //然后重新插入一条这个当前状态数据
                    wSqlStringList.add(DMSDeviceStatusDAO.getInstance().DMS_InsertActiveStatusString(wDMSDeviceStatus));

                    wIsCurrent = false;
                }
                //如果历史状态未变更但时长变更最后需要更新一下历史设备状态时长 包括详情时长 用最后这个状态时间来减去开始时间
                if (wIsCurrent) {
                    //更新当前历史状态时长
                    wSqlStringList.add(DMSDeviceStatusDAO.getInstance().DMS_KeepActiveStatusString(wDMSDeviceStatus));

                }
                //更新当前历史详情状态时长  用最后一个时间来更新
                wSqlStringList.addAll(DMSDeviceStatusDAO.getInstance().DMS_KeepDetailStatusString(wCurrentDeviceStatusDetailDic.values(), wDMSDeviceStatus.StatusTimeEnd));

                //更新或插入当前设备状态
                wSqlStringList.add(DMSDeviceStatusDAO.getInstance().DMS_UpdateCurrentStatusString(wDMSDeviceStatus));

            }

            DMSDeviceStatusDAO.getInstance().ExecuteSqlTransaction(wSqlStringList);
        } catch (Exception e) {
            wResult.FaultCode += e.getMessage();
            log.error("DMS_SyncDeviceStatusHistoryList", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> DMS_CloseDeviceAlarmAll(BMSEmployee wLoginUser, String wAssetNo) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            List<String> wSqlStringList = DMSDeviceAlarmDAO.getInstance().DMS_CloseAlarmAll(wAssetNo);

            DMSDeviceAlarmDAO.getInstance().ExecuteSqlTransaction(wSqlStringList);
        } catch (Exception e) {
            wResult.FaultCode += e.getMessage();
            log.error("DMS_CloseDeviceAlarmAll", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> DMS_SyncDeviceAlarm(BMSEmployee wLoginUser, String wAssetNo, String wAlarmCode, int wEventType, int wIsCode) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            List<String> wSqlStringList = new ArrayList<String>();

            DMSDeviceAlarm wDMSDeviceAlarm = new DMSDeviceAlarm();

            wDMSDeviceAlarm.AssetNo = wAssetNo;
            wDMSDeviceAlarm.AlarmCode = wAlarmCode;
            wDMSDeviceAlarm.EventType = wEventType;

            if (wEventType == 1) {
                wDMSDeviceAlarm.StatusTime = Calendar.getInstance();
                if (wIsCode == 1) {
                    wSqlStringList.addAll(DMSDeviceAlarmDAO.getInstance().DMS_CloseAlarmCodeHistoryString(wAssetNo));
                    wSqlStringList.add(DMSDeviceAlarmDAO.getInstance().DMS_DeleteAlarmCodeString(wAssetNo));

                }
                //删除当前报警
                wSqlStringList.add(DMSDeviceAlarmDAO.getInstance().DMS_DeleteAlarmString(wAssetNo, wAlarmCode));
                //新增当前报警
                wSqlStringList.add(DMSDeviceAlarmDAO.getInstance().DMS_InsertCurrentAlarmString(wDMSDeviceAlarm, wIsCode));
                //新增历史报警
                wSqlStringList.add(DMSDeviceAlarmDAO.getInstance().DMS_InsertAlarmHistoryString(wDMSDeviceAlarm));
            } else {
                wDMSDeviceAlarm.StatusTimeEnd = Calendar.getInstance();
                if (wIsCode == 1) {
                    wSqlStringList.addAll(DMSDeviceAlarmDAO.getInstance().DMS_CloseAlarmCodeHistoryString(wAssetNo));
                    wSqlStringList.add(DMSDeviceAlarmDAO.getInstance().DMS_DeleteAlarmCodeString(wAssetNo));
                } else {

                    wSqlStringList.addAll(DMSDeviceAlarmDAO.getInstance().DMS_CloseAlarmHistoryString(wDMSDeviceAlarm));
                    //删除当前报警
                    wSqlStringList.add(DMSDeviceAlarmDAO.getInstance().DMS_DeleteAlarmString(wAssetNo, wAlarmCode));


                }


            }

            DMSDeviceAlarmDAO.getInstance().ExecuteSqlTransaction(wSqlStringList);


        } catch (Exception e) {
            wResult.FaultCode += e.getMessage();
            log.error("DMS_SyncDeviceAlarm", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<DMSDeviceParameter> DMS_SyncDeviceAlarm(BMSEmployee wLoginUser, int wDeviceID, String wAssetNo, String wAlarmCode) {
        ServiceResult<DMSDeviceParameter> wResult = new ServiceResult<DMSDeviceParameter>();
        try {
            if (StringUtils.isEmpty(wAlarmCode))
                return wResult;

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            DMSDeviceParameter wDMSDeviceParameter = new DMSDeviceParameter();
            wDMSDeviceParameter.AssetNo = wAssetNo;
            wDMSDeviceParameter.DeviceID = wDeviceID;
            wDMSDeviceParameter.VariableName = StringUtils.Format("AlarmAuto_{0}", wAlarmCode);
            wDMSDeviceParameter.Name = StringUtils.Format("报警编号_{0}", wAlarmCode);
            wDMSDeviceParameter.DataType = ((int) DMSDataTypes.String.getValue());
            wDMSDeviceParameter.DataAction = ((int) DMSDataActions.Default.getValue());
            wDMSDeviceParameter.DataClass = ((int) DMSDataClass.Alarm.getValue());
            wDMSDeviceParameter.DataLength = wAlarmCode.length();
            wDMSDeviceParameter.Description = wAlarmCode;
            wDMSDeviceParameter.Active = 1;

            DMSDeviceParameterDAO.getInstance().UpdatePo(wLoginUser, wDMSDeviceParameter, wErrorCode);

            wResult.AddFaultCode(wErrorCode);
            if (StringUtils.isNotEmpty(wResult.FaultCode)) {
                return wResult;
            }
            wResult.Result = wDMSDeviceParameter;


            List<String> wSqlStringList = new ArrayList<String>();

            DMSDeviceAlarm wDMSDeviceAlarm = new DMSDeviceAlarm();
            wDMSDeviceAlarm.StatusTime = Calendar.getInstance();
            wDMSDeviceAlarm.StatusTime = Calendar.getInstance();
            wDMSDeviceAlarm.AssetNo = wAssetNo;
            wDMSDeviceAlarm.AlarmCode = wDMSDeviceParameter.Code;
            wDMSDeviceAlarm.EventType = 1;
            //新增当前报警
            wSqlStringList.add(DMSDeviceAlarmDAO.getInstance().DMS_InsertCurrentAlarmString(wDMSDeviceAlarm, 1));
            //新增历史报警
            wSqlStringList.add(DMSDeviceAlarmDAO.getInstance().DMS_InsertAlarmHistoryString(wDMSDeviceAlarm));

            DMSDeviceAlarmDAO.getInstance().ExecuteSqlTransaction(wSqlStringList);

        } catch (Exception e) {
            wResult.FaultCode += e.getMessage();
            log.error("DMS_SyncDeviceAlarm", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<String>> DMS_SyncDeviceAlarmList(BMSEmployee wLoginUser, List<String> wAssetNoList,
                                                               List<DMSDeviceAlarm> wDMSDeviceAlarmList) {
        ServiceResult<List<String>> wResult = new ServiceResult<List<String>>();
        try {
            wResult.Result = new ArrayList<String>();
            if (wAssetNoList == null || wAssetNoList.size() <= 0)
                return wResult;

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            List<DMSDeviceLedger> wDMSDeviceLedgerList = CacheUtils.GetCacheTList(DMSDeviceLedger.class);

            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
            wDMSDeviceLedgerList.sort(Comparator.comparing(DMSDeviceLedger::getActive).reversed().thenComparing(DMSDeviceLedger::getEditTime));

            Map<String, DMSDeviceLedger> wDMSDeviceLedgerDic = wDMSDeviceLedgerList.stream().collect(Collectors.toMap(p -> p.AssetNo, p -> p, (o1, o2) -> o1));


            if (wDMSDeviceAlarmList == null)
                wDMSDeviceAlarmList = new ArrayList<DMSDeviceAlarm>();

            wDMSDeviceAlarmList.sort((o1, o2) -> o2.StatusTime.compareTo(o1.StatusTime));

            Map<String, Map<String, DMSDeviceAlarm>> wDMSDeviceAlarmListDic
                    = wDMSDeviceAlarmList.stream().collect(Collectors.groupingBy(p -> p.AssetNo, Collectors.toMap(q -> q.AlarmCode, q -> q, (o1, o2) -> o1)));


            List<DMSDeviceAlarm> wDMSDeviceAlarmListSource = DMSDeviceAlarmDAO.getInstance().DMS_CurrentDeviceAlarmList(wLoginUser, null, wAssetNoList, wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }

            wDMSDeviceAlarmListSource.sort((o1, o2) -> o2.StatusTime.compareTo(o1.StatusTime));

            Map<String, Map<String, DMSDeviceAlarm>> wDMSDeviceAlarmListSourceDic = wDMSDeviceAlarmListSource.stream().collect(Collectors.groupingBy(p -> p.AssetNo,
                    Collectors.toMap(q -> q.AlarmCode, q -> q, (o1, o2) -> o1)));


            List<String> wSqlStringList = new ArrayList<String>();


            //对比报警将消失的报警插入历史报警eventtype=2

            for (String wAssetNo : wAssetNoList) {
                if (!wDMSDeviceLedgerDic.containsKey(wAssetNo)) {
                    wResult.Result.add(StringUtils.Format("AssetNo:{0} Not Found!",
                            wAssetNo));
                    continue;
                }

                if (!wDMSDeviceAlarmListDic.containsKey(wAssetNo)) {

                    if (!wDMSDeviceAlarmListSourceDic.containsKey(wAssetNo))
                        continue;
                    //删除当前报警
                    wSqlStringList.add(DMSDeviceAlarmDAO.getInstance().DMS_DeleteAlarmString(wAssetNo));

                    //关闭历史报警
                    wSqlStringList.addAll(DMSDeviceAlarmDAO.getInstance().DMS_CloseAlarmHistoryString(wDMSDeviceAlarmListSourceDic.get(wAssetNo).values()));

                    continue;
                }
                if (!wDMSDeviceAlarmListSourceDic.containsKey(wAssetNo))
                    wDMSDeviceAlarmListSourceDic.put(wAssetNo, new HashMap<String, DMSDeviceAlarm>());


                for (String wAlarmCode : wDMSDeviceAlarmListDic.get(wAssetNo).keySet()) {
                    if (wDMSDeviceAlarmListSourceDic.get(wAssetNo).containsKey(wAlarmCode))
                        continue;
                    //新增当前报警
                    wSqlStringList.add(DMSDeviceAlarmDAO.getInstance().DMS_InsertCurrentAlarmString(wDMSDeviceAlarmListDic.get(wAssetNo).get(wAlarmCode), 0));
                    //新增历史报警
                    wSqlStringList.add(DMSDeviceAlarmDAO.getInstance().DMS_InsertAlarmHistoryString(wDMSDeviceAlarmListDic.get(wAssetNo).get(wAlarmCode)));
                }

                for (String wAlarmCode : wDMSDeviceAlarmListSourceDic.get(wAssetNo).keySet()) {
                    if (wDMSDeviceAlarmListDic.get(wAssetNo).containsKey(wAlarmCode))
                        continue;
                    // 关闭历史报警
                    wSqlStringList.addAll(DMSDeviceAlarmDAO.getInstance().DMS_CloseAlarmHistoryString(wDMSDeviceAlarmListSourceDic.get(wAssetNo).get(wAlarmCode)));
                    //删除当前报警
                    wSqlStringList.add(DMSDeviceAlarmDAO.getInstance().DMS_DeleteAlarmString(wAssetNo, wAlarmCode));

                }
            }
            DMSDeviceStatusDAO.getInstance().ExecuteSqlTransaction(wSqlStringList);
        } catch (Exception e) {
            wResult.FaultCode += e.getMessage();
            log.error("DMS_SyncDeviceAlarmList", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<String>> DMS_SyncDeviceAlarmHistoryList(BMSEmployee wLoginUser, List<DMSDeviceAlarm> wDMSDeviceAlarmList) {
        ServiceResult<List<String>> wResult = new ServiceResult<List<String>>();
        try {
            wResult.Result = new ArrayList<String>();
            if (wDMSDeviceAlarmList == null || wDMSDeviceAlarmList.size() <= 0)
                return wResult;

            DMSDeviceStatusDAO.getInstance().ExecuteSqlTransaction(DMSDeviceAlarmDAO.getInstance().DMS_AddAlarmHistoryString(wDMSDeviceAlarmList));
        } catch (Exception e) {
            wResult.FaultCode += e.getMessage();
            log.error("DMS_SyncDeviceAlarmHistoryList", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> DMS_SyncDeviceRealParameter(BMSEmployee wLoginUser, String wAssetNo, String wParameterCode, String wParameterValue) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            if (StringUtils.isEmpty(wAssetNo) || StringUtils.isEmpty(wParameterCode))
                return wResult;
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);


            DMSDeviceRealParameter wDMSDeviceRealParameter = DMSDeviceRealParameterDAO.getInstance().DMS_SelectDeviceRealParameter(wLoginUser, 0, wParameterCode, wErrorCode);
            if (wDMSDeviceRealParameter == null || wDMSDeviceRealParameter.ParameterID <= 0)
                return wResult;

            wDMSDeviceRealParameter.ParameterValue = wParameterValue;

            wDMSDeviceRealParameter.UpdateTime = Calendar.getInstance();
            DMSDeviceStatusDAO.getInstance().ExecuteSqlTransaction(DMSDeviceRealParameterDAO.getInstance().DMS_UpdateDeviceRealParameter(wDMSDeviceRealParameter));
        } catch (Exception e) {
            wResult.FaultCode += e.getMessage();
            log.error("DMS_SyncDeviceRealParameter", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<String>> DMS_SyncDeviceRealParameterList(BMSEmployee wLoginUser, List<DMSDeviceRealParameter> wDMSDeviceRealParameterList) {
        ServiceResult<List<String>> wResult = new ServiceResult<List<String>>();
        try {
            wResult.Result = new ArrayList<String>();
            if (wDMSDeviceRealParameterList == null || wDMSDeviceRealParameterList.size() <= 0)
                return wResult;
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);


            Map<String, List<DMSDeviceRealParameter>> wDMSDeviceRealParameterListDic = wDMSDeviceRealParameterList.stream().collect(Collectors.groupingBy(p -> p.AssetNo));

            List<String> wAssetNoList = wDMSDeviceRealParameterListDic.keySet().stream().collect(Collectors.toList());

            List<DMSDeviceRealParameter> wDMSDeviceRealParameterSourceList = DMSDeviceRealParameterDAO.getInstance().DMS_SelectDeviceRealParameterList(wLoginUser, null, wAssetNoList, wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;

            }
            Map<String, Map<String, DMSDeviceRealParameter>> wDMSDeviceRealParameterSourceListDic =
                    wDMSDeviceRealParameterSourceList.stream().collect(Collectors.groupingBy(DMSDeviceRealParameter::getAssetNo, Collectors.toMap(q -> q.ParameterCode, q -> q, (o1, o2) -> o1)));


            List<String> wSqlStringList = new ArrayList<String>();

            List<DMSDeviceAlarm> wDMSDeviceAlarmList = new ArrayList<DMSDeviceAlarm>();
            DMSDeviceAlarm wDMSDeviceAlarm = null;

            for (String wAssetNo : wAssetNoList) {
                if (!wDMSDeviceRealParameterSourceListDic.containsKey(wAssetNo)) {
                    wResult.Result.add(StringUtils.Format("AssetNo:{0} Parameter List Not Found!", wAssetNo));
                    continue;
                }

                for (DMSDeviceRealParameter wDMSDeviceRealParameter : wDMSDeviceRealParameterListDic.get(wAssetNo)) {
                    if (!wDMSDeviceRealParameterSourceListDic.get(wAssetNo).containsKey(wDMSDeviceRealParameter.ParameterCode)) {
                        //添加或修改

                        wResult.Result.add(StringUtils.Format("AssetNo:{0} ParameterCode:{1}  Not Found!", wAssetNo, wDMSDeviceRealParameter.ParameterCode));
                        continue;
                    }


                    if (wDMSDeviceRealParameter.UpdateTime.get(Calendar.YEAR) <= 2010)
                        wDMSDeviceRealParameter.UpdateTime = Calendar.getInstance();

                    if ((wDMSDeviceRealParameterSourceListDic.get(wAssetNo).get(wDMSDeviceRealParameter.ParameterCode).DataClass == (int) DMSDataClass.Alarm.getValue())
                            && (StringUtils.parseInt(wDMSDeviceRealParameter.ParameterValue) == 1)) {
                        //处理报警数据
                        wDMSDeviceAlarm = new DMSDeviceAlarm();
                        wDMSDeviceAlarm.AlarmCode = wDMSDeviceRealParameterSourceListDic.get(wAssetNo).get(wDMSDeviceRealParameter.ParameterCode).ParameterCode;
                        wDMSDeviceAlarm.AlarmDesc = wDMSDeviceRealParameterSourceListDic.get(wAssetNo).get(wDMSDeviceRealParameter.ParameterCode).ParameterDesc;
                        wDMSDeviceAlarm.AlarmName = wDMSDeviceRealParameterSourceListDic.get(wAssetNo).get(wDMSDeviceRealParameter.ParameterCode).ParameterName;
                        wDMSDeviceAlarm.AlarmVariableName = wDMSDeviceRealParameterSourceListDic.get(wAssetNo).get(wDMSDeviceRealParameter.ParameterCode).VariableName;
                        wDMSDeviceAlarm.DeviceID = wDMSDeviceRealParameterSourceListDic.get(wAssetNo).get(wDMSDeviceRealParameter.ParameterCode).DeviceID;
                        wDMSDeviceAlarm.AssetNo = wDMSDeviceRealParameterSourceListDic.get(wAssetNo).get(wDMSDeviceRealParameter.ParameterCode).AssetNo;
                        wDMSDeviceAlarm.DeviceName = wDMSDeviceRealParameterSourceListDic.get(wAssetNo).get(wDMSDeviceRealParameter.ParameterCode).DeviceName;
                        wDMSDeviceAlarm.StatusTime = wDMSDeviceRealParameter.UpdateTime;
                        wDMSDeviceAlarmList.add(wDMSDeviceAlarm);
                    }

                    if (wDMSDeviceRealParameterSourceListDic.get(wAssetNo).get(wDMSDeviceRealParameter.ParameterCode).ParameterValue.equals(wDMSDeviceRealParameter.ParameterValue)) {
                        continue;
                    }

                    wDMSDeviceRealParameterSourceListDic.get(wAssetNo).get(wDMSDeviceRealParameter.ParameterCode).ParameterValue = wDMSDeviceRealParameter.ParameterValue;
                    wDMSDeviceRealParameterSourceListDic.get(wAssetNo).get(wDMSDeviceRealParameter.ParameterCode).UpdateTime = wDMSDeviceRealParameter.UpdateTime;

                    wSqlStringList.add(DMSDeviceRealParameterDAO.getInstance().DMS_UpdateDeviceRealParameter(
                            wDMSDeviceRealParameterSourceListDic.get(wAssetNo).get(wDMSDeviceRealParameter.ParameterCode)));


                }
            }
            DMSDeviceStatusDAO.getInstance().ExecuteSqlTransaction(wSqlStringList);

            this.DMS_SyncDeviceAlarmList(wLoginUser, wAssetNoList, wDMSDeviceAlarmList);

        } catch (Exception e) {
            wResult.FaultCode += e.getMessage();
            log.error("DMS_SyncDeviceRealParameterList", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> DMS_SyncProcessRecord(BMSEmployee wLoginUser, String wAssetNo, String wWorkpieceNo, String wOrderNo, String wProductNo, Map<String, DMSProcessRecordItem> wDMSProcessRecordItemDic) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            if (StringUtils.isEmpty(wAssetNo) || wDMSProcessRecordItemDic == null || wDMSProcessRecordItemDic.size() <= 0)
                return wResult;

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            // 获取设备
            DMSDeviceLedger wDMSDeviceLedger = DMSDeviceLedgerDAO.getInstance().DMS_SelectDeviceLedger(wLoginUser, -1, "", wAssetNo, wErrorCode);

            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
            if (wDMSDeviceLedger == null || wDMSDeviceLedger.ID <= 0)
                return wResult;

            DMSProcessRecord wDMSProcessRecord = new DMSProcessRecord();
            wDMSProcessRecord.AssetNo = wDMSDeviceLedger.AssetNo;
            wDMSProcessRecord.DeviceID = wDMSDeviceLedger.ID;
            wDMSProcessRecord.DeviceName = wDMSDeviceLedger.Name;
            wDMSProcessRecord.DeviceNo = wDMSDeviceLedger.Code;
            wDMSProcessRecord.EndTime = wDMSProcessRecordItemDic.containsKey("EndTime") ? StringUtils.parseCalendar(wDMSProcessRecordItemDic.get("EndTime").ParameterValue) : Calendar.getInstance();
            wDMSProcessRecord.StartTime = wDMSProcessRecordItemDic.containsKey("StartTime") ? StringUtils.parseCalendar(wDMSProcessRecordItemDic.get("StartTime").ParameterValue) : Calendar.getInstance();
            wDMSProcessRecord.ProductNo = wDMSProcessRecordItemDic.containsKey("ProductNo") ? StringUtils.parseString(wDMSProcessRecordItemDic.get("ProductNo").ParameterValue) : "";
            wDMSProcessRecord.OrderNo = wDMSProcessRecordItemDic.containsKey("OrderNo") ? StringUtils.parseString(wDMSProcessRecordItemDic.get("OrderNo").ParameterValue) : "";
            wDMSProcessRecord.ProductName = wDMSProcessRecordItemDic.containsKey("ProductName") ? StringUtils.parseString(wDMSProcessRecordItemDic.get("ProductName").ParameterValue) : "";
            wDMSProcessRecord.Remark = wDMSProcessRecordItemDic.containsKey("Remark") ? StringUtils.parseString(wDMSProcessRecordItemDic.get("Remark").ParameterValue) : "";
            wDMSProcessRecord.Status = wDMSProcessRecordItemDic.containsKey("Status") ? StringUtils.parseInt(wDMSProcessRecordItemDic.get("Status").ParameterValue) : 1;
            wDMSProcessRecord.StatusText = wDMSProcessRecordItemDic.containsKey("StatusText") ? StringUtils.parseString(wDMSProcessRecordItemDic.get("StatusText").ParameterValue) : "";
            wDMSProcessRecord.WorkpieceNo = wDMSProcessRecordItemDic.containsKey("WorkpieceNo") ? StringUtils.parseString(wDMSProcessRecordItemDic.get("WorkpieceNo").ParameterValue) : "";

            wDMSProcessRecord.WorkpieceNo = StringUtils.isEmpty(wDMSProcessRecord.WorkpieceNo) ? wWorkpieceNo : wDMSProcessRecord.WorkpieceNo;
            wDMSProcessRecord.OrderNo = StringUtils.isEmpty(wDMSProcessRecord.OrderNo) ? wOrderNo : wDMSProcessRecord.OrderNo;
            wDMSProcessRecord.ProductNo = StringUtils.isEmpty(wDMSProcessRecord.ProductNo) ? wProductNo : wDMSProcessRecord.ProductNo;

            wDMSProcessRecord.ItemList = wDMSProcessRecordItemDic.values().stream().collect(Collectors.toList());


            for (DMSProcessRecordItem item : wDMSProcessRecord.ItemList) {
                item.DeviceID = wDMSProcessRecord.DeviceID;
                item.DeviceNo = wDMSProcessRecord.DeviceNo;
                item.AssetNo = wDMSProcessRecord.AssetNo;
            }

            //region  避免重复信号
            //获取此设备最新的一条记录
            List<DMSProcessRecord> wDMSProcessRecordList = DMSProcessRecordDAO.getInstance().DMS_CurrentProcessRecordList(wLoginUser,
                    wDMSProcessRecord.DeviceID, wDMSProcessRecord.DeviceNo, wDMSProcessRecord.AssetNo,
                    wDMSProcessRecord.WorkpieceNo, Pagination.Create(1, 1), wErrorCode);

            if (wDMSProcessRecordList != null && wDMSProcessRecordList.size() > 0 && wDMSProcessRecordList.get(0) != null
                    && wDMSProcessRecordList.get(0).ID > 0 && StringUtils.isNotEmpty(wDMSProcessRecordList.get(0).WorkpieceNo)
                    && wDMSProcessRecordList.get(0).WorkpieceNo.equals(wDMSProcessRecord.WorkpieceNo)) {

                //若最新的数据开始时间或结束时间与此相同 则视为重复
                double wTimeSub = CalendarTool.SubSeconds(wDMSProcessRecordList.get(0).StartTime, wDMSProcessRecord.StartTime);
                if (wTimeSub <= 2 && wTimeSub >= -2) {
                    return wResult;
                }
                wTimeSub = CalendarTool.SubSeconds(wDMSProcessRecordList.get(0).EndTime, wDMSProcessRecord.EndTime);

                if (wTimeSub <= 2 && wTimeSub >= -2) {
                    return wResult;
                }
            }

            //endregion

            DMSProcessRecordDAO.getInstance().DMS_InsertProcessRecord(wLoginUser, wDMSProcessRecord, wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);

            }

        } catch (Exception e) {
            wResult.FaultCode += e.getMessage();
            log.error("DMS_SyncProcessRecord", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<String>> DMS_SyncProcessRecordList(BMSEmployee wLoginUser, List<DMSProcessRecord> wDMSProcessRecordList) {
        ServiceResult<List<String>> wResult = new ServiceResult<List<String>>();
        try {
            wResult.Result = new ArrayList<String>();
            if (wDMSProcessRecordList == null || wDMSProcessRecordList.size() <= 0)
                return wResult;

            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            // 获取设备列表
            List<DMSDeviceLedger> wDMSDeviceLedgerList = CacheUtils.GetCacheTList(DMSDeviceLedger.class);


            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
            wDMSDeviceLedgerList.sort(Comparator.comparing(DMSDeviceLedger::getActive).reversed().thenComparing(p -> p.EditTime));

            Map<String, DMSDeviceLedger> wDMSDeviceLedgerDic = wDMSDeviceLedgerList.stream().collect(Collectors.toMap(p -> p.AssetNo, p -> p, (o1, o2) -> o1));


            //根据订单号 获取订单对象
            List<String> wOrderNoList = wDMSProcessRecordList.stream().map(p -> p.OrderNo).distinct().collect(Collectors.toList());

            wOrderNoList.removeIf(p -> StringUtils.isEmpty(p));


            //根据車号 获取订单对象


            List<String> wAssetNoList = wDMSProcessRecordList.stream().map(p -> p.AssetNo).distinct().collect(Collectors.toList());

            wAssetNoList.removeIf(p -> StringUtils.isEmpty(p));


            Map<String, List<DMSProcessRecord>> wDMSProcessRecordListDic = wDMSProcessRecordList.stream().collect(Collectors.groupingBy(p -> p.AssetNo));

            for (String wAssetNo : wDMSProcessRecordListDic.keySet()) {
                if (StringUtils.isEmpty(wAssetNo))
                    continue;
                //直接插入数据库  插入前 写更新Active=0 历史记录
                if (!wDMSDeviceLedgerDic.containsKey(wAssetNo)) {
                    wResult.Result.add(StringUtils.Format("AssetNo:{0} Not Found!",
                            wAssetNo));
                    continue;
                }
                for (DMSProcessRecord wDMSProcessRecord : wDMSProcessRecordListDic.get(wAssetNo)) {


                    DMSProcessRecordDAO.getInstance().DMS_InsertProcessRecord(wLoginUser, wDMSProcessRecord, wErrorCode);
                    if (wErrorCode.Result != 0) {
                        wResult.Result.add(StringUtils.Format("AssetNo:{0} OrderNo:{1} MetroNo:{2} WorkPieceNo:{3}  Update Error:{4}",
                                wAssetNo, wDMSProcessRecord.OrderNo, wDMSProcessRecord.MetroNo, wDMSProcessRecord.WorkpieceNo, ExceptionTypes.getEnumType(wErrorCode.get()).getLabel()));
                    }

                }
            }


        } catch (Exception e) {
            wResult.FaultCode += e.getMessage();
            log.error("DMS_SyncProcessRecordList", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<DMSDeviceStatistics>> DMS_SelectDeviceStatusDetailStatisticsTime(BMSEmployee wLoginUser,
                                                                                               int wLineID, List<Integer> wDeviceIDList, String wAssetNo, int wStatType,
                                                                                               Calendar wStartTime, Calendar wEndTime) {
        ServiceResult<List<DMSDeviceStatistics>> wResult = new ServiceResult<List<DMSDeviceStatistics>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            wResult.Result = DMSDeviceStatusStatisticsDAO.getInstance().DMS_SelectDeviceStatusDetailStatisticsTime(wLoginUser, wLineID, wDeviceIDList, wAssetNo,
                    wStatType, wStartTime, wEndTime, wErrorCode);

            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
            }

        } catch (Exception e) {
            wResult.FaultCode += e.getMessage();
            log.error("DMS_SelectDeviceStatusDetailStatisticsTime", e);
        }
        return wResult;
    }


    @Override
    public ServiceResult<List<Map<String, Object>>> DMS_SelectProcessRecordParams(BMSEmployee wLoginUser, String wDeviceCode, Calendar wStartTime, Calendar wEndTime) {
        ServiceResult<List<Map<String, Object>>> wResult = new ServiceResult<List<Map<String, Object>>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            wResult.Result = DMSDeviceRealParameterDAO.getInstance().DMS_SelectProcessRecordParams(wLoginUser, wDeviceCode, wStartTime, wEndTime, wErrorCode);

            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_SelectProcessRecordParams", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> DMS_InitDeviceTable(BMSEmployee wLoginUser) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            List<DMSDeviceLedger> wDMSDeviceLegerList = CacheUtils.GetCacheTList(DMSDeviceLedger.class);

            if (wErrorCode.Result != 0) {
                log.info("DMS_InitDeviceTable   获取设备台账列表失败! error:{}", ExceptionTypes.getEnumType(wErrorCode.get()).getLabel());
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
            String wDeviceCode = "";
            for (DMSDeviceLedger wDMSDeviceLeger : wDMSDeviceLegerList) {
                if (wDMSDeviceLeger == null || wDMSDeviceLeger.ID <= 0 || StringUtils.isEmpty(wDMSDeviceLeger.Code))
                    continue;
                wDeviceCode = wDMSDeviceLeger.ChangeToTableName();
                DMSDeviceParameterDAO.getInstance().DMS_InitDeviceTable(wLoginUser, (DMSDataClass.TechnologyData.getValue()), -1, wDMSDeviceLeger.ID, wDeviceCode, wErrorCode);

                DMSDeviceParameterDAO.getInstance().DMS_InitDeviceTable(wLoginUser, (DMSDataClass.WorkParams.getValue()), -1, wDMSDeviceLeger.ID, wDeviceCode, wErrorCode);
                //DMSDeviceParameterDAO.getInstance().DMS_InitDeviceTable(wLoginUser, (DMSDataClass.WorkParams.getValue()), -1, wDMSDeviceLeger.ID, wDeviceCode, wDMSDeviceLeger.ModelID, wErrorCode);

                DMSDeviceParameterDAO.getInstance().DMS_InitDeviceTable(wLoginUser, (DMSDataClass.QualityParams.getValue()), -1, wDMSDeviceLeger.ID, wDeviceCode, wErrorCode);
                //DMSDeviceParameterDAO.getInstance().DMS_InitDeviceTable(wLoginUser, (DMSDataClass.QualityParams.getValue()), -1, wDMSDeviceLeger.ID, wDeviceCode, wDMSDeviceLeger.ModelID, wErrorCode);

                DMSDeviceParameterDAO.getInstance().DMS_InitDeviceTable(wLoginUser, wDMSDeviceLeger.ModelID, wDeviceCode, wErrorCode);

                DMSDeviceParameterDAO.getInstance().DMS_InitDeviceTable(wLoginUser, (DMSDataClass.Params.getValue()), 1, wDMSDeviceLeger.ID, wDeviceCode, wErrorCode);

            }

        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error("DMS_InitDeviceTable", ex);
        }
        return wResult;
    }


}
